package com.bluebench.bigul.utility.annotation;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bluebench.bigul.domain.Element;
import com.bluebench.bigul.exception.GraphicInitializationException;


public class AnnotationUtil {
	
	public static final String FIELD_NAME="FIELD_NAME";
	public static final String VALUE="VALUE";

	@SuppressWarnings("unchecked")
	public static List<Map<String,Object>> scanField(Class<?> annotatedClass, Class annotationClass){
				
		AnnotatedElement[] anns = annotatedClass.getDeclaredFields();
		
		List<Map<String,Object>> fieldProperties = new ArrayList<Map<String,Object>>();
		for(AnnotatedElement an : anns){
			
			if(an.isAnnotationPresent(annotationClass)){
				Map<String,Object> fieldPropertyMap = new HashMap<String,Object>();
				String fieldName = ((Field)an).getName();
				fieldPropertyMap.put(FIELD_NAME, fieldName);	
				Assign assign = an.getAnnotation(annotationClass);
				fieldPropertyMap.put("VALUE",assign.value());	
				fieldProperties.add(fieldPropertyMap);				
			}			
			
		}
		
		return fieldProperties;
	}
	
	
	
	public static void main(String[] args) throws GraphicInitializationException {
		Element e = new Element();
		Class<?> annotatedClass = e.getClass();
		List<Map<String,Object>> fields = scanField(annotatedClass, Assign.class);
		
	}
	
}
